package com.yc.utilslib.asynctask;

import android.nfc.tech.MifareClassic;
import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;

/**
 * 作者：TanTun
 * 时间：2017/3/2
 * 邮箱：32965926@qq.com
 * 描述：卡片断开连接
 */

public class NfcDisconnect extends AsyncTask<MifareClassic, Void, Boolean> {
    private AsyncTaskCallBack mCallback;

    public interface AsyncTaskCallBack {
        void onTaskOver(Boolean isDisconnect);
    }

    public void setAsyncTaskCallBack(AsyncTaskCallBack callback) {
        mCallback = callback;
    }

    @Override
    protected Boolean doInBackground(MifareClassic... params) {

        Boolean isDisconnect = false;

        try {
            if (params != null && params.length > 0) {
                while (params[0].isConnected()) {
                    Thread.sleep(100);
                }
                params[0].close();
                Log.v("NFC", "关闭NFC");
                isDisconnect = true;
            }
        } catch (java.lang.InterruptedException | IOException e) {
            e.printStackTrace();
        }

        return isDisconnect;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        mCallback.onTaskOver(result);
    }
}
